package com.ie.sevice.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.OrdersEntity;
import com.ie.mapper.admin.OrdersMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, OrdersEntity> implements OrdersService {

    @Override
    public ResponseResult<Map<String, Object>> getAllOrders() {
        // 创建分页对象 iPage (PageSize=5)
        IPage<OrdersEntity> page = new Page<>(1, 5); // 默认第1页，每页5条

        // 创建 LambdaQueryWrapper 条件构造器
        LambdaQueryWrapper<OrdersEntity> wrapper = new LambdaQueryWrapper<>();

        // 判断 ordersEntity.id 是否为 null && id > 0?
        Long id = null; // 假设这里从参数中获取 id，此处为示例直接赋值为 null
        if (id != null && id > 0) {
            // 添加 id 等于条件到 Wrapper
            wrapper.eq(OrdersEntity::getId, id);
        }

        // 执行分页查询 page(iPage, wrapper)
        IPage<OrdersEntity> pageResult = this.page(page, wrapper);

        // 创建返回结果 Map
        Map<String, Object> data = new HashMap<>();
        // 将查询结果放入 Map("allOrders")
        data.put("allOrders", pageResult.getRecords());
        // 将总页数放入 Map("totalPage")
        data.put("totalPage", pageResult.getPages());

        // 返回成功响应结果
        return ResponseResult.getSuccessResult(data);
    }
}